package com.jfinal.club._admin.dajuan;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.jfinal.club._admin.Handwriting.Handwriting;
import com.jfinal.club._admin.same.Same;
import com.jfinal.club.common.model.Dajuan;
import com.jfinal.club.common.model.Dajuanxiangqing;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

public class DajuanAdminService {

	private Dajuan dao = new Dajuan().dao();
	private Dajuanxiangqing dajuanxiangqingDao = new Dajuanxiangqing().dao();
	private Object dajuan;

	public Page<Dajuan> paginate(int pageNum) {
		return dao.paginate(pageNum, 10, "select shiti.id,shiti.question,exam.id as examid,exam.ename,dajuanxiangqing.vurl",
				"from shiti left join exam on shiti.exid=exam.id left join dajuanxiangqing on shiti.id=dajuanxiangqing.tid order by shiti.id desc");
	}
	public Page<Dajuan> score(int pageNum,int sid) {
		return dao.paginate(pageNum, 10, "select exam.id,tname,ename,hscore ",
				"from dajuan,exam,teacher  where dajuan.examid=exam.id and exam.tid= teacher.id and dajuan.sid=?",sid);
	}

	public boolean exists(int dajuanId, String name) {
		name = name.toLowerCase().trim();
		String sql = "select id from dajuan where question = ? and id != ? limit 1";
		Integer id = Db.queryInt(sql, name, dajuanId);
		return id != null;
	}

	public List<Dajuan> getAll() {
		List<Dajuan> list = dao.find("select * from dajuan order by question");
		return list;
	}

	public Integer getDajuan(int examid,int sid) {
//		lockNumber = lockNumber.toLowerCase().trim();
		String sql = "select id from dajuan where examid = ? and sid = ? limit 1";
		Integer idRet = Db.queryInt(sql, examid,sid);
		return idRet;
	}

	public Ret update(int examid,int sid, int tid, UploadFile uf, Dajuanxiangqing dajuanxiangqing) {
		if (uf == null) {
			return Ret.fail("msg", "上传文件不能为空！");
		}
		try {Dajuan dajuan;
			
			Integer idRet = getDajuan(examid,sid);
			if (idRet==null){
			    dajuan = new Dajuan();
				dajuan.setSid(sid);
				dajuan.setExamid(examid);
				dajuan.setStime(new Date());
			}else {
				dajuan=dao.findById(idRet);
			}		
			dajuan.setEtime(new Date());
			if (dajuan.getId() == null) {
				dajuan.save();
			} else {
				dajuan.update();
			}
			
			// Dajuanxiangqing dajuanxiangqing = new Dajuanxiangqing();
			dajuanxiangqing.setPid(dajuan.getId());
			dajuanxiangqing.setTid(tid);
			dajuanxiangqing.setUtime(new Date());
			dajuanxiangqing.save();
			// return Ret.ok("msg", "创建成功");
			String datiUrl = "/upload" + getDatiTempDir() + dajuanxiangqing.getId() + "_" + System.currentTimeMillis()
					+ ".jpg";
			String saveFile = PathKit.getWebRootPath() + datiUrl;
			File file = uf.getFile();
			file.renameTo(new File(saveFile));
			dajuanxiangqing.setVurl(datiUrl);
			dajuanxiangqing.update();
			return Ret.ok("vurl", datiUrl);
		} catch (Exception e) {
			return Ret.fail("msg", e.getMessage());
		}
	}

	public String getDatiTempDir() {
		return "/dajuan/";
	}

	public int getDatiMaxSize() {
		return 1024 * 1024;
	}

	public  Ret Handwriting(){
		 List<Dajuanxiangqing> list = dajuanxiangqingDao.find("select vurl,id from dajuanxiangqing ORDER BY id ");
		 Handwriting hw = new Handwriting();
		 for(int i=0;i<list.size();i++) {
		 String result =hw.Handwriting(PathKit.getWebRootPath() + list.get(i).getStr("vurl"));
		 Dajuanxiangqing dj = dajuanxiangqingDao.findById(list.get(i).getInt("id"));
		 System.out.println(list.get(i).getInt("id"));
		 dj.setText(result);
		 dj.update();
		 try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		 }
		 return Ret.ok("msg", "阅卷成功");
	}
	public Ret compare() {
    	List<Dajuanxiangqing> list = dajuanxiangqingDao.find("select dajuanxiangqing.text as text,shiti.answer as answer,dajuanxiangqing.id as id from dajuanxiangqing right join shiti  on shiti.id=dajuanxiangqing.tid where not ISNULL(text) and LENGTH(trim(text))>1");
         Same same = new Same();
         for(int i=0;i<list.size();i++) {
        	int result =same.compare(list.get(i).getStr("text"),list.get(i).getStr("answer"),"CNN");
        	Dajuanxiangqing djxq = dajuanxiangqingDao.findById(list.get(i).getInt("id"));
        	if(djxq!=null) {
        	djxq.setScore(result);
        	djxq.update();  
        	 }
           }  	
         return Ret.ok("msg", "评分成功"); 	
		}
	public Ret plus() {
		int result = Db.update("UPDATE dajuan set hscore=(SELECT sum(score) from dajuanxiangqing where pid=dajuan.id ) ");
		return Ret.ok("msg", "评分成功"+result); 	
	}
	
}
